<#
This creates a web server on port 800 that runs given commands
It servers up output from this script.
example: http://localhost:8000/?cmd=dir c:\

#>

$url = 'http://localhost:8000/'
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add($url)
$listener.Start()

Write-Host "Listening at $url..."

while ($listener.IsListening)
{
    $context = $listener.GetContext()
    $requestUrl = $context.Request.Url
    $response = $context.Response
    
    Write-Host ''
    Write-Host "> $requestUrl"

    $localPath = $requestUrl.LocalPath
    
    
    $cmd = $requestURL -split "cmd=";
    $cmd = $cmd[1].split();
    
    Write-Host "Executing: cmd /c $cmd"
    
    $OutputVariable = & cmd /c $cmd | Out-String
    
    Write-Host "$OutputVariable"
    
    
    $buffer = [System.Text.Encoding]::UTF8.GetBytes($OutputVariable)
    $response.ContentLength64 = $buffer.Length
    $response.OutputStream.Write($buffer, 0, $buffer.Length)
    
    
    $response.Close()

    $responseStatus = $response.StatusCode
    
}